// 关于英雄的一些操作
library Hero initializer init requires InitFunction

    globals
        // 英雄是否存活  true表示存活  false表示死亡
        boolean array hero_is_alive
        // 英雄复活时间
        real array heroAliveTime
        // 英雄死亡次数
        integer array heroDeadCount

        // 英雄死亡时的坐标
        real array die_x
        real array die_y

        // 英雄每个等级所需的经验
        integer array heroLevelNeedExp

        // 天赋等级
        integer array heroTalentLevel
    endglobals


    // 计算英雄制定级别升级所需要的经验值
    function computeLevelNeedExp takes integer level returns integer
        // 固定因素
        local integer ex1 = 0
        // 等级因素
        local integer ex2 = 100
        // 上一个值因素
        local integer ex3 = 1
        
        if level <= 1 then
            return 0
        endif

        if heroLevelNeedExp[level] == 0 then
            set heroLevelNeedExp[level] = ex1 + ex2 * level + (ex3 - 1) * computeLevelNeedExp(level - 1)
            // set heroLevelNeedExp[level] = ex1 + ex2 * level
        endif

        return heroLevelNeedExp[level]
    endfunction

    // 获取指定级别英雄所需总经验值
    function getLevelNeedExp takes integer level returns integer
        local integer loopi 
        local integer exp = 0
        set loopi = 1
        loop
            exitwhen loopi > level
            set exp = exp + heroLevelNeedExp[loopi]
            set loopi = loopi + 1
        endloop
        return exp
    endfunction

    // 判断英雄是否存活 true表示存活  false表示死亡
    function heroIsAlive takes integer uid returns boolean
        return hero_is_alive[uid]
    endfunction

    // 减少英雄复活时间，最少为4秒 true表少修改成功
    function reduceHeroAliveTime takes integer uid, real time returns boolean
        
        if (heroAliveTime[uid] - time >= 4.0) then
            set heroAliveTime[uid] = heroAliveTime[uid] - time
            return true
        endif
        
        if (heroAliveTime[uid] - time < 4.0) then
            set heroAliveTime[uid] = 4.0
            return false
        endif

        return false
    endfunction

    // 增加英雄复活时间
    function addHeroAliveTime takes integer uid, real time returns nothing
        set heroAliveTime[uid] = heroAliveTime[uid] + time
    endfunction


    // =============================================================
    // 复活英雄
    function reviveHero takes integer uid, unit u, real x, real y returns nothing

        // 删除墓碑
        call DestroyEffect(LoadEffectHandle(gameht, StringHash("死亡墓碑"), uid))

        // 复活英雄
        call ReviveHero(u, x, y, true)
        // 标记状态  表示复活
        set hero_is_alive[uid] = true
        // 选中单位
        call SelectUnitForPlayerSingle( u, Player(uid) )
        // 移动镜头持续0秒
        if (GetLocalPlayer() == Player(uid)) then
            // Use only local code (no net traffic) within this block to avoid desyncs.
            call PanCameraToTimed(x, y, 0)
        endif
    endfunction

    function timer_heroAlive takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local integer tid = GetHandleId(t)
        local integer uid = LoadInteger(gameht, tid, 1)
        local unit u = LoadUnitHandle(gameht, tid, 2)
        local timerdialog tdg = LoadTimerDialogHandle(gameht, tid, 3)
        // local real x = GetRectCenterX(getHomeRect())
        // local real y = GetRectCenterY(getHomeRect())
        local real x = 0
        local real y = 0

        // 复活英雄
        call reviveHero(uid, u, x, y)

        call FlushChildHashtable(gameht, tid)
        call DestroyTimer(t)
        call DestroyTimerDialog(tdg)
        set t = null
        set u = null
        set tdg = null
    endfunction

    // 英雄死亡action
    function heroUnitDieAction takes nothing returns nothing

        local integer uid = GetPlayerId(GetOwningPlayer(GetDyingUnit()))
        local timer t
        local integer tid
        local timerdialog tdg
        // 记录死亡次数
        set heroDeadCount[uid] = heroDeadCount[uid] + 1
        // 标记状态  表示死亡
        set hero_is_alive[uid] = false

        set die_x[uid] = GetUnitX(GetDyingUnit())
        set die_y[uid] = GetUnitY(GetDyingUnit())
        call SaveEffectHandle(gameht, StringHash("死亡墓碑"), uid, AddSpecialEffect("tx_shizijia.mdl", die_x[uid], die_y[uid]))

        set t = CreateTimer()
        set tdg = CreateTimerDialogBJ( t, (GetUnitName(GetDyingUnit()) + "复活时间"))
        set tid = GetHandleId(t)
        call SaveInteger(gameht, tid, 1, uid)
        call SaveUnitHandle(gameht, tid, 2, GetDyingUnit())
        call SaveTimerDialogHandle(gameht, tid, 3, tdg)
        call TimerStart(t, heroAliveTime[uid], false, function timer_heroAlive)

        set t = null
        set tdg = null
        // call BJDebugMsg("死亡单位：" + GetUnitName(GetDyingUnit()))
    endfunction

    private function init takes nothing returns nothing
        local integer uid
        set uid = 0
        loop
            exitwhen uid > PLAYER_NUMBER
            set hero_is_alive[uid] = true
            // 表示初始复活时间为8秒
            set heroAliveTime[uid] = 8.0
            set uid = uid + 1
        endloop

        // 计算英雄1-1000级升级所需的经验
        call computeLevelNeedExp(1000)

    endfunction
endlibrary
